package com.hn658.groupshare.frontend.action.verify.impl;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.hn658.framework.common.AbstractBaseService;
import com.hn658.framework.common.IServiceResponse;
import com.hn658.framework.logging.enums.LogCategory;
import com.hn658.framework.shared.exception.BusinessException;
import com.hn658.groupshare.common.enums.VerifyCodeType;
import com.hn658.groupshare.common.vo.CommonResponse;
import com.hn658.groupshare.frontend.action.verify.IVerifyCodeAction;
import com.hn658.groupshare.frontend.action.verify.vo.SendVerifyCodeRequest;
import com.hn658.groupshare.security.service.ISmsService;
import com.hn658.groupshare.security.service.IVerifyCodeService;

/**
 * 
 * @author ztjie
 * 
 */
@Controller("verifyCodeAction")
@Path("verify")
@Produces("application/json; charset=UTF-8")
@Consumes("application/json; charset=UTF-8")
public class VerifyCodeActionImpl extends AbstractBaseService implements
		IVerifyCodeAction {

	@Autowired
	private IVerifyCodeService verifyCodeService;

	@Autowired
	private ISmsService smsService;

	@Path("sendVerifyCode")
	@POST
	public IServiceResponse sendVerifyCode(SendVerifyCodeRequest request) {
		CommonResponse response = new CommonResponse();
		try {
			String code = verifyCodeService.createAndSaveMobileVerifyCode(
					request.getMobilePhone(), VerifyCodeType
							.getVerifyCodeType(request.getVerifyCodeType()));
			smsService.sendVerifyCode(request.getMobilePhone(), code);
			logger.debug(
					LogCategory.SYSTEM,
					String.format("[%s]验证码发送：[%s],",
							new Object[] { request.getMobilePhone(), code }));
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}
}
